﻿using SpaceNeedle.City6473.Web.Data.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpaceNeedle.City6473.Web.Data.Models
{
    public partial class RequestItem
    {
        public string Permalink
        {
            get { return "http://6473.azurewebsites.net/solicitacao/" + this.Id; }
        }

        public string Base64Photo { get; set; }
        public string CreatedByName { get; set; }

        public string Thumbnail
        {
            get { return this.Photo.Replace(".jpg", "_260x180.jpg"); }
        }

        public RequestItem Clone()
        {
            RequestItem item = new RequestItem();
            item.Address = this.Address;
            item.App = this.App;
            item.Base64Photo = this.Base64Photo;
            item.CreatedBy = this.CreatedBy;
            item.CreatedByApp = this.CreatedByApp;
            
            if (this.User != null)
            {
                item.CreatedByName = Text.AbbreviateName(this.User.Name);
            }
            
            item.CreationDate = this.CreationDate;
            item.Description = this.Description;
            item.Geoposition = this.Geoposition;
            item.Id = this.Id;
            item.Photo = this.Photo;
            item.RequestId = this.RequestId;
            item.Status = this.Status;
            item.StatusInfo = this.StatusInfo;
            item.UpdateDate = this.UpdateDate;
            item.UpdatedBy = this.UpdatedBy;
            item.UserTypedAddress = this.UserTypedAddress;
            item.Number = this.FormatNumber(this.Number);

            return item;
        }

        private string FormatNumber(string number)
        {
            try
            {
                if (number.Length < 8)
                {
                    return number;
                }

                string year = number.Substring(0, 4);
                string month = number.Substring(4, 2);
                string symbol = number.Substring(6, 2);
                string code = number.Substring(8);

                return string.Format("{0}-{1}-{2}-{3}", year, month, symbol, code);
            }
            catch { }

            return number;
        }
    }
}
